import * as fs from "node:fs";
import * as path from "node:path";
import { Image, Pixel } from "../hp3-img.js";
import { loadPng, savePng } from "../hp3-png.js";

/**
 * 使用区域插值算法缩小图像
 * @param src 源图像
 * @param newWidth 新的宽度
 * @param newHeight 新的高度
 * @returns 缩小后的图像
 */
export function formatImageInfo(src: Image):string{
    let minX = src.width;
    let maxX = 0;
    let minY = src.height;
    let maxY = 0;
    for (let y = 0; y < src.height; y++) {
        for (let x = 0; x < src.width; x++) {
            const p = src.pixels[src.width*y+x];
            if(p.a>0){
                minX = Math.min(minX,x);
                maxX = Math.max(maxX,x);
                minY = Math.min(minY,y);
                maxY = Math.max(maxY,y);
            }
        }
    }
    return [src.width,src.height,minX,minY,maxX+1-minX,maxY+1-minY].join(",");
}

const inputDir = "E:\\gamedev\\resources\\拼格子塔防\\角色\\序列帧\\ok-auto";
const pattern = /.+\.png/;

// 获取输入目录中的所有文件
const files = fs.readdirSync(inputDir);

// 处理每个PNG文件
for (const file of files) {
    if(file.match(pattern)){
        const inputFile = path.join(inputDir, file);
        const src = loadPng(inputFile);
        const info = formatImageInfo(src);
        console.log(file+","+info);
    }
}
